package com.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取来自注册页面的用户名和密码
       String uname =  req.getParameter("uname");
       String pwd = req.getParameter("pwd");

       resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

       //判空
        if (uname !=null && uname.length()>0 && pwd != null && pwd.length() >0 ) {
            //用户名和密码有值
            //暂存用户名和密码
            HttpSession session = req.getSession();
            session.setAttribute("username",uname);
            session.setAttribute("password",pwd);

            resp.sendRedirect("login.jsp");

        } else {
            out.println("用户名或密码为空");
        }




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
